<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("domain/TagDomain.php");
include_once("domain/AttributeDomain.php");
include_once("dao/CommonDAO.php");
include_once("dao/TagDAO.php");

class TagImpl extends CommonUtils implements TagDAO, CommonDAO
{

	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag = $DBTag;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{
		$sql  = 'SELECT id, categoryId, name, alias, searchName, nameURL, descript, r_recommend, nameId, r_popularity ';
		$sql .= "FROM tag WHERE id = $id";
		$conn = null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$oTag = new TagDomain();

				$oTag->setId($row[0]);
				$oTag->setCategoryId($row[1]);
				$oTag->setName($row[2]);
				$oTag->setAlias($row[3]);
				$oTag->setSearchName($row[4]);
				$oTag->setNameURL($row[5]);
				$oTag->setDescript($row[6]);
				$oTag->setRRecommend($row[7]);
				$oTag->setNameId($row[8]);
				
			} else {
				$this->messages = "get coupon data failed by Id. sql : $sql <br>";
				$this->messages .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $oTag;
	}

	public function getByNameId($nameid)
	{
		$sql  = 'SELECT id, categoryId, name, alias, searchName, nameURL, descript, r_recommend ';
		$sql .= "FROM tag WHERE nameId = '$nameid'";
		$conn = null;
		$oTag = null;
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$oTag = new TagDomain();

				$oTag->setId($row[0]);
				$oTag->setCategoryId($row[1]);
				$oTag->setName($row[2]);
				$oTag->setAlias($row[3]);
				$oTag->setSearchName($row[4]);
				$oTag->setNameURL($row[5]);
				$oTag->setDescript($row[6]);
				$oTag->setRRecommend($row[7]);
				$oTag->setNameId($nameid);				
				
			} else {
				$this->messages = "get coupon data failed by Id. sql : $sql <br>";
				$this->messages .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $oTag;
	}
	
	public function add($oTag)
	{
		
		if($oTag instanceof TagDomain){
			$IDkey = md5($oTag->getMerchantId() . $oTag->getCode() . $oTag->getAmount() . $oTag->getDescript());
			
			$sql 	 = "INSERT INTO tag SET modifiedTime = UTC_TIMESTAMP(), descript='";
			$sql	.= addslashes($oTag->getDescript());
			
			$sql 	.= "', comments='";
			$sql	.= addslashes($oTag->getComments());
	
			$sql 	.= "', amount='";
			$sql	.= addslashes($oTag->getAmount());
	
			$sql 	.= "', merchantId=";
			$sql	.= $oTag->getMerchantId();

			$sql 	.= ", categoryId=";
			$sql	.= $oTag->getCategoryId();
			
			$sql	.= ", expireDate='";
			$sql	.= $oTag->getExpireDate();

			$sql	.= "', code='";
			$sql	.= $oTag->getCode();

			$sql	.= "', IDkey='";
			$sql	.= $IDkey;
			
			$sql	.= "', status=";
			$sql	.= $oTag->getStatus();
			$sql	.= "";
			
			$id = $this->insert($this->DBTag, $sql);
			
			$oTag->setId($id);			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of TagDomain.';
		}
		
		return $id;
	}
	
	/* delete tag
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM tag WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function getTags($page = 0, $discount = 20)
	{
		$oTags = $this->getCustoms(" WHERE status = 1 AND r_couponCount > 0", " order by r_recommend desc, r_couponCount DESC", $page, $discount);
		
		foreach($oTags as $oTag){
			$this->getAttributes($oTag);
			
//			echo "tag attriubtes : " . $oTag->getAttribute(1, 52)->getAttributeValue() . "<br>";
		}
		
		return $oTags;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql 	= "SELECT count(1) FROM tag $where";	
			$rs 	= mysql_query($sql, $conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){
				$sql  = 'SELECT id, categoryId, name, alias, searchName, nameURL, descript, r_recommend, nameId';
				$sql .= " FROM tag  $where $order";
				$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn);
				if($rs){
					while($row = mysql_fetch_array($rs)){
						$oTag = new TagDomain();

						$oTag->setId($row[0]);
						$oTag->setCategoryId($row[1]);
						$oTag->setName($row[2]);
						$oTag->setAlias($row[3]);
						$oTag->setSearchName($row[4]);
						$oTag->setNameURL($row[5]);
						$oTag->setDescript($row[6]);
						$oTag->setRRecommend($row[7]);
						$oTag->setNameId($row[8]);
						
						$arrback[] = $oTag;
					}
				}else {
					$this->message = "select failed. sql : $sql";
					$this->message .= "<br>error: " . mysql_error($conn);
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $arrback;
	}
	
	public function getAttributes($oTag)
	{
		$arrback 	= array();
		$conn 		= null;
		
		$sql  = "SELECT sourceId, attributeId, attributeValue FROM tag_attributes WHERE tagId = " . $oTag->getId();
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			while ($row = mysql_fetch_row($rs)) {
				$oAttribute = new AttributeDomain();

				$oAttribute->setSourceId($row[0]);
				$oAttribute->setAttributeId($row[1]);
				$oAttribute->setAttributeValue($row[2]);
				
				$arrback[$row[0] . '_' . $row[1]] = $oAttribute;
			}
			
			$oTag->setAttributes($arrback);
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $arrback;
	}
	
	public function getTagCouponCount($DBTag)
	{
		$sql  = 'SELECT ct.tagId, COUNT(1) FROM coupon c, coupon_tag ct WHERE c.id = ct.couponId AND ct.authorized = 1 AND c.status = 1 GROUP BY ct.tagId';
			
		return $this->getHash($DBTag, $sql);
	}
	
	public function getHasCouponTags($DBTag)
	{
		$sql  = 'SELECT id, r_couponCount FROM tag WHERE status = 1 AND r_couponCount > 0';
			
		return $this->getHash($DBTag, $sql);
	}
	
	public function getMessage() 
	{
		return $this->message;
	}

	
	
}
?>
